package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.mock.dao.TownDao;
import com.mock.model.Town;
import com.mock.utils.HibernateUtil;

public class TownDaoImp extends GenericDaoImpl<Town, Long> implements TownDao{


	@Override
	public List<Town> getAllTown() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Town> list = session.createQuery("from Town").list();
		session.flush();
		session.getTransaction().commit();
		return list;
	}

	@Override
	public Town getById(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Town town = (Town)session.load(Town.class, id);
		if(null != town)
			return town;
		return null;
	}

	@Override
	public Town getByTown(String townName) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from Town t where t.townName =:townName");
		query.setParameter("townName", townName);
		return (Town)query.uniqueResult();
	}

}
